package com.astrob.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;

import com.astrob.naviframe.Start;

public class SystemSetFileHandle {
	private static String SYSSETPATH = Start.RUNDIR + "/systemset.dat";
	private static SystemSetFileHandle instance = new SystemSetFileHandle();
	private SystemSetData mSystemSet = new SystemSetData();

	public static SystemSetFileHandle getInstance() {
		return instance;
	}

	public SystemSetFileHandle() {
		if (Start.RUNDIR.length() <= 0) {
			Start.getInstance().checkRundir();
			SYSSETPATH = Start.RUNDIR + "/systemset.dat";
		}
		load();
	}

	public SystemSetData getSystemSet() {
		return mSystemSet;
	}

	@SuppressWarnings("unchecked")
	public void load() {
		mSystemSet.previewLon = 121.476455;
		mSystemSet.previewLat = 31.231706;

		File file = new File(SYSSETPATH);
		if (null == file || file.exists() == false)
			return;

		try {
			FileInputStream fileInputStream = new FileInputStream(file);
			ObjectInputStream objInputStream = new ObjectInputStream(
					fileInputStream);

			if (fileInputStream.available() > 0)
				mSystemSet = (SystemSetData) objInputStream.readObject();

			mSystemSet.havePic = false;
			mSystemSet.queryowner = 0;
			// mSystemSet.nMapScreenMode = 1;
			objInputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (StreamCorruptedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public void save() {
		@SuppressWarnings("rawtypes")
		File file = new File(SYSSETPATH);
		if (file.exists())
			file.delete();

		try {
			ObjectOutputStream objOutPutStream = new ObjectOutputStream(
					new FileOutputStream(file));
			objOutPutStream.writeObject(mSystemSet);
			objOutPutStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void revert() {
		mSystemSet.voiceNavi = true;
		mSystemSet.speedcamera = true;
		mSystemSet.naviMapViewMode = 0; // 0 --- 2D 1 --- 3D
		mSystemSet.naviCarposMode = 0; // BOTTOMCENTER = 0, CENTER = 1
		mSystemSet.securityNaviVoice = true; // 新手安全导航语音
		mSystemSet.SCVoice = true; // // 本地电子眼语音（SC.SMF?)，默认true
		mSystemSet.opertionSound = true; // 操作音-->已改为draw TMC
		mSystemSet.naviScale = 100000; // 导航比例尺
		mSystemSet.favoritesSound = true; // 收藏夹语音
		mSystemSet.reRounteCalSound = true; // 重新路径演算语音
		mSystemSet.SpeedWarning = true; // 超速警语音
		mSystemSet.languageNaviVoice = 1; // 1--普通话导航

		mSystemSet.speedLimitHWIndex = 1; // 高速 120, 140, 160, 180
		mSystemSet.speedLimitGDIndex = 1; // 国道 70, 80, 100, 120
		mSystemSet.speedLimitPMIndex = 1; // 其他道路 60,70, 80, 90, 100

		mSystemSet.carType = 0; // 0-机动车 1-行人 2-非机动车
		mSystemSet.turnRestriction = true; // 转弯限制
		mSystemSet.fastest = true; // 最快路径（最短路径 ）
		mSystemSet.toll = false; // true:尽量避开收费站
		mSystemSet.highway = true; // 高速公路优先（平面道路）-- 备选路线暂未用到此设置
		mSystemSet.ferry = true; // 可以走轮渡

		mSystemSet.fromDN = 8; // [3~9]
		mSystemSet.toDN = 18; // [17~22]
		mSystemSet.language = 0; // china = 0, english

		mSystemSet.showRCV = true;
		mSystemSet.showSearchMenu = true;
		mSystemSet.DNMode = 2;
		mSystemSet.mapstyle = 11; // 10 -- 日 1--- 夜
		mSystemSet.carstyle = 3; // 红色三角车标

		mSystemSet.showTMC = true; // 显示实时路况，同下面的TMCing， 道路通行能力显示
		mSystemSet.showTSP = true; // 显示路况看板
		mSystemSet.showFav = true; // 显示收藏点
		mSystemSet.showSC = true; // 显示电子眼
		mSystemSet.showMsn = true; // 显示车友位置

		mSystemSet.useTSP = true; // 实时路况功能
		mSystemSet.TMCing = true; // 道路通行能力显示， 已被上面的showTMC替换
		// mSystemSet.Eventing = true; // 交通事件显示
		mSystemSet.EventFVing = true; // 自由导航临时事件播报
		mSystemSet.EventSVing = true; // 播报选中的事件信息
		mSystemSet.updateTspTime = 5 * 60; // 刷新频率 -- 5分钟

		mSystemSet.isRouteManage = false;

		mSystemSet.isStartReNavi = false;
		mSystemSet.isShowCurRoad = false;

		// querycity = QString("上海市");
		mSystemSet.isSaveMapScreenMode = true;
		mSystemSet.nMapScreenMode = 1;

		mSystemSet.isYunEEyesSound = true; //
		mSystemSet.isFavEeyesSound = true; //

		save();
	}
}
